#!/usr/bin/env python
import re
import sys
import os
import time
# the version number
version_number = "0.0.0"
try:
	version_number = sys.argv[1]
except:
	print "Usage: setversion.py 2.9.99\n"
	sys.exit(-1)
# used when building on winders
if ( version_number == 'choose' ) or ( version_number == 'same' ):
	fullpath = "c:\ede\common.py"
	f=open( fullpath, 'r' )
	filebody=f.read()
	f.close()
	pat = re.compile( "^version_number\s*=(.*)$", re.M )
	matches = pat.findall( filebody )
	if len( matches ) != 0:
		old_version_number = version_number
		version_number = matches[0]
		version_number = version_number.replace('"', '').strip()
		if old_version_number != 'same':
			import wx
			app = wx.PySimpleApp()
			dlg = wx.TextEntryDialog( None, 'Set the version number', 'Version Number', version_number)
			res = dlg.ShowModal()
			version_number = dlg.GetValue()
			dlg.Destroy()
			if res != wx.ID_OK:
				print "Canceled..."
				sys.exit()
			
if version_number == "0.0.0": sys.exit(-1)
# the build number
l = time.localtime()
build_number   = "%s-%s-%s-%s-%s" % ( l[0], l[1], l[2], l[3], l[4] )

# Common
pat = re.compile( "^version_number\s*=.*$", re.M )
pat2 = re.compile( "^build_number\s*=.*$", re.M )
fullpath = os.getcwd() + "/common.py"
f=open( fullpath, 'r' )
filebody=f.read()
f.close()
filebody = pat.sub( "version_number\t= \"%s\"" % version_number, filebody )
filebody = pat2.sub( "build_number\t= \"%s\"" % build_number, filebody )
f=open( fullpath, 'w' )
f.write( filebody )
f.close()


# InnoSetup
#~ VersionInfoVersion=2.9.11
#~ OutputBaseFilename=ede-2.9.11
pat = re.compile( "^AppVerName=Etelos Development Environment .*$", re.M )
pat2 = re.compile( "^VersionInfoVersion=.*$", re.M )
pat3 = re.compile( "^OutputBaseFilename=.*$", re.M )
fullpath = os.getcwd() + "/ede.iss"
f=open( fullpath, 'r' )
filebody=f.read()
f.close
filebody = pat.sub( "AppVerName=Etelos Development Environment %s" % version_number, filebody )
filebody = pat2.sub( "VersionInfoVersion=%s" % version_number, filebody )
filebody = pat3.sub( "OutputBaseFilename=ede-%s" % ( version_number ), filebody )
f=open( fullpath, 'w' )
f.write( filebody )
f.close
